package com.ruoyi.web.controller.api;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictTypeService;

/**
 * 小程序字典数据接口
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/dict")
public class MiniappDictController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 根据字典类型查询字典数据信息
     * 
     * @param dictType 字典类型
     * @return 字典数据列表
     */
    @Anonymous
    @GetMapping("/type/{dictType}")
    public AjaxResult getDictByType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    /**
     * 获取所有字典类型列表
     * 
     * @return 字典类型列表
     */
    @Anonymous
    @GetMapping("/types")
    public AjaxResult getDictTypes()
    {
        List<com.ruoyi.common.core.domain.entity.SysDictType> dictTypeList = dictTypeService.selectDictTypeAll();
        List<String> dictTypes = new ArrayList<>();
        for (com.ruoyi.common.core.domain.entity.SysDictType dictType : dictTypeList)
        {
            dictTypes.add(dictType.getDictType());
        }
        return success(dictTypes);
    }

    /**
     * 批量获取多个字典类型的数据
     * 
     * @param dictTypes 字典类型列表，用逗号分隔
     * @return 字典数据Map
     */
    @Anonymous
    @GetMapping("/batch/{dictTypes}")
    public AjaxResult getBatchDict(@PathVariable String dictTypes)
    {
        String[] types = dictTypes.split(",");
        java.util.Map<String, List<SysDictData>> result = new java.util.HashMap<>();
        
        for (String dictType : types)
        {
            if (StringUtils.isNotEmpty(dictType))
            {
                List<SysDictData> data = dictTypeService.selectDictDataByType(dictType.trim());
                if (StringUtils.isNull(data))
                {
                    data = new ArrayList<SysDictData>();
                }
                result.put(dictType.trim(), data);
            }
        }
        
        return success(result);
    }

    /**
     * 获取村级管理系统相关字典数据
     * 
     * @return 村级管理系统字典数据
     */
    @Anonymous
    @GetMapping("/village")
    public AjaxResult getVillageDicts()
    {
        java.util.Map<String, List<SysDictData>> result = new java.util.HashMap<>();
        
        // 工作状态字典
        List<SysDictData> workStatus = dictTypeService.selectDictDataByType("work_status");
        if (StringUtils.isNull(workStatus))
        {
            workStatus = new ArrayList<SysDictData>();
        }
        result.put("work_status", workStatus);
        
        // 反馈状态字典
        List<SysDictData> feedbackStatus = dictTypeService.selectDictDataByType("feedback_status");
        if (StringUtils.isNull(feedbackStatus))
        {
            feedbackStatus = new ArrayList<SysDictData>();
        }
        result.put("feedback_status", feedbackStatus);
        
        // 问题类型字典
        List<SysDictData> problemType = dictTypeService.selectDictDataByType("problem_type");
        if (StringUtils.isNull(problemType))
        {
            problemType = new ArrayList<SysDictData>();
        }
        result.put("problem_type", problemType);

        // 村小组字典
        List<SysDictData> villageGroup = dictTypeService.selectDictDataByType("village_group");
        if (StringUtils.isNull(villageGroup))
        {
            villageGroup = new ArrayList<SysDictData>();
        }
        result.put("village_group", villageGroup);

        // 快递状态字典
        List<SysDictData> expressStatus = dictTypeService.selectDictDataByType("express_status");
        if (StringUtils.isNull(expressStatus))
        {
            expressStatus = new ArrayList<SysDictData>();
        }
        result.put("express_status", expressStatus);
        
        return success(result);
    }
}
